package cn.hello2018.oa.biz.impl;

import cn.hello2018.oa.biz.ClaimVoucherBiz;
import cn.hello2018.oa.dao.ClaimVoucherDao;
import cn.hello2018.oa.dao.ClaimVoucherItemDao;
import cn.hello2018.oa.dao.DealRecordDao;
import cn.hello2018.oa.dao.EmployeeDao;
import cn.hello2018.oa.entity.ClaimVoucher;
import cn.hello2018.oa.entity.ClaimVoucherItem;
import cn.hello2018.oa.entity.DealRecord;
import cn.hello2018.oa.entity.Employee;
import cn.hello2018.oa.global.Contant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author jeymingwu
 * @date 2019/2/19 14:36
 */
@Service
public class ClaimVoucherBizImpl implements ClaimVoucherBiz {

    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private ClaimVoucherDao claimVoucherDao;
    @Autowired
    private ClaimVoucherItemDao claimVoucherItemDao;
    @Autowired
    private DealRecordDao dealRecordDao;

    public void save(ClaimVoucher claimVoucher, List<ClaimVoucherItem> items) {
        claimVoucher.setCreateTime(new Date());
        claimVoucher.setNextDealSn(claimVoucher.getCreateSn());
        claimVoucher.setStatus(Contant.CLAIMVOUCHER_CREATED);
        claimVoucherDao.insert(claimVoucher);

        for (ClaimVoucherItem item : items) {
            item.setClaimVoucherId(claimVoucher.getId());
            claimVoucherItemDao.insert(item);
        }

        insertDealRecord(claimVoucher.getId(), claimVoucher.getCreateSn(), Contant.DEAL_CREATE,
                Contant.CLAIMVOUCHER_CREATED, null);
    }

    public void edit(ClaimVoucher claimVoucher, List<ClaimVoucherItem> items) {
        claimVoucher.setStatus(Contant.CLAIMVOUCHER_EDIT);
        claimVoucher.setNextDealSn(claimVoucher.getCreateSn());
        claimVoucherDao.update(claimVoucher);

        List<ClaimVoucherItem> olds = claimVoucherItemDao.selectByClaimVoucherId(claimVoucher.getId());
        // 删除已删除的条目
        for (ClaimVoucherItem old : olds) {
            boolean isHave = false;
            for (ClaimVoucherItem item : items) {
                if (item.getId() == old.getId()) {
                    isHave = true;
                    break;
                }
            }
            if (!isHave) {
                claimVoucherItemDao.delete(old.getId());
            }
        }
        // 修改或新增条目
        for (ClaimVoucherItem item : items) {
            item.setClaimVoucherId(claimVoucher.getId());
            if (item.getId() != null && item.getId() > 0) {
                claimVoucherItemDao.update(item);
            } else {
                claimVoucherItemDao.insert(item);
            }
        }

        insertDealRecord(claimVoucher.getId(), claimVoucher.getCreateSn(), Contant.DEAL_UPDATE,
                Contant.CLAIMVOUCHER_EDIT, null);
    }

    public void sumbit(int id) {
        ClaimVoucher claimVoucher = claimVoucherDao.select(id);
        claimVoucher.setStatus(Contant.CLAIMVOUCHER_SUBMITED);
        Employee employee = employeeDao.select(claimVoucher.getCreateSn());
        claimVoucher.setNextDealSn(employeeDao.selectByDepartmentAndPost(employee.getDepartmentSn(), Contant.POST_FM).get(0).getSn());
        claimVoucherDao.update(claimVoucher);

        insertDealRecord(claimVoucher.getId(), claimVoucher.getNextDealSn(), Contant.DEAL_SUMMIT,
                Contant.CLAIMVOUCHER_SUBMITED, null);
    }

    public void deal(DealRecord dealRecord) {
        ClaimVoucher claimVoucher = claimVoucherDao.select(dealRecord.getClaimVoucherId());
        Employee employee = employeeDao.select(claimVoucher.getNextDealSn());

        // 判断是否需要复核
        if (dealRecord.getDealWay().equals(Contant.DEAL_PASS)) {
            if (claimVoucher.getTotalAmount() <= Contant.LIMIT_CHECK || employee.getPost().equals(Contant.POST_GM)) {
                // 不需要复审 或者
                // 审核人是总经理
                // 审核金额<所规定的金额
                claimVoucher.setStatus(Contant.CLAIMVOUCHER_APPROVED);
                claimVoucher.setNextDealSn(employeeDao.selectByDepartmentAndPost(null, Contant.POST_CASHIER).get(0).getSn());
            } else {
                // 需要复审
                claimVoucher.setStatus(Contant.CLAIMVOUCHER_RECHECK);
                claimVoucher.setNextDealSn(employeeDao.selectByDepartmentAndPost(null, Contant.POST_GM).get(0).getSn());
            }
            insertDealRecord(claimVoucher.getId(), employee.getSn(), Contant.DEAL_PASS,
                    claimVoucher.getStatus(), dealRecord.getComment());
        } else if (dealRecord.getDealWay().equals(Contant.DEAL_BACK)) {
            // 打回操作
            claimVoucher.setStatus(Contant.CLAIMVOUCHER_BACK);
            claimVoucher.setNextDealSn(claimVoucher.getCreateSn());
            insertDealRecord(claimVoucher.getId(), employee.getSn(), Contant.DEAL_BACK, Contant.CLAIMVOUCHER_BACK, dealRecord.getComment());
        } else if (dealRecord.getDealWay().equals(Contant.DEAL_REJECT)) {
            // 拒绝
            claimVoucher.setStatus(Contant.CLAIMVOUCHER_TERMINATED);
            claimVoucher.setNextDealSn(null);
            insertDealRecord(claimVoucher.getId(), employee.getSn(), Contant.DEAL_REJECT,
                    Contant.CLAIMVOUCHER_TERMINATED, dealRecord.getComment());
        } else if (dealRecord.getDealWay().equals(Contant.DEAL_PAID)) {
            // 打款
            claimVoucher.setStatus(Contant.CLAIMVOUCHER_PAID);
            claimVoucher.setNextDealSn(null);
            insertDealRecord(claimVoucher.getId(), employee.getSn(), Contant.DEAL_PAID, Contant.CLAIMVOUCHER_PAID, dealRecord.getComment());
        }
        claimVoucherDao.update(claimVoucher);

    }

    // 插入记录（报销单、处理类型、处理结果）
    public void insertDealRecord(Integer cvid, String dealSn, String way, String result, String comment) {
        DealRecord dealRecord = new DealRecord();
        dealRecord.setClaimVoucherId(cvid); // 报销单编号
        dealRecord.setDealSn(dealSn); // 处理人编号
        dealRecord.setDealTime(new Date()); // 处理时间
        dealRecord.setDealWay(way); // 处理类型
        dealRecord.setDealResult(result); // 处理结果
        dealRecord.setComment(comment); // 备注
        dealRecordDao.insert(dealRecord);
    }

    public ClaimVoucher get(Integer id) {
        return claimVoucherDao.select(id);
    }

    public List<ClaimVoucherItem> getItems(Integer cvid) {
        return claimVoucherItemDao.selectByClaimVoucherId(cvid);
    }

    public List<DealRecord> getRecords(Integer cvid) {
        return dealRecordDao.selectByClaimVoucherId(cvid);
    }

    public List<ClaimVoucher> getForSelf(String sn) {
        return claimVoucherDao.selectByCreateSn(sn);
    }

    public List<ClaimVoucher> getForDeal(String sn) {
        return claimVoucherDao.selectByNextDealSn(sn);
    }
}
